package edu.neu.cs6515.hci;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import javax.swing.JLabel;
import javax.swing.JPanel;

import lombok.Getter;
import lombok.Setter;

import edu.neu.cs6515.board.HexagonBoard;
import edu.neu.cs6515.gui.PlayerRenderer;
import edu.neu.cs6515.xml.Placement;
import edu.neu.cs6515.xml.Score;
import edu.neu.cs6515.xml.Tile;

/**
 * User Interface for HCIStrategy
 */
public class HCIPanel extends JPanel {

    private static final long serialVersionUID = 1L;


    @Getter
    private PlayerRenderer playerRenderer;

    private EventsCoordinator eventsCoordinator;

    @Getter
    @Setter
    private BlockingQueue<List<Placement>> blockingQueue;

    /**
     * C'tor for HCIPanel
     *
     * @param hciStrategy
     *            the HCIStrategy
     */
    public HCIPanel(final int interval) {
        blockingQueue = new ArrayBlockingQueue<List<Placement>>(1);
        eventsCoordinator = new EventsCoordinator(
                this, interval);
		this.initializeComponents();
    }

	/**
	 * -------------------------------
	 *  |                            |
	 *  |                            |
	 *  |                            |
	 *  |  {@link PlayerRenderer}    |
	 *  |                            |
	 *  |                            |
	 *  |                            |
	 * -------------------------------
	 *  initialize components for HCIPanel
	 */
	public void initializeComponents() {
        playerRenderer = new PlayerRenderer();
        this.setLayout(new BorderLayout());
        this.add(playerRenderer,
                BorderLayout.CENTER);
	}	

    /**
     * update HCIPanel according to the content of HCIStrategy
     * @param scoreBeforePlacement 
     * @param tilesInHand 
     * @param hexagonBoard 
     */
    public void update(final HexagonBoard hexagonBoard, 
            final List<Tile> tilesInHand, final Score scoreBeforePlacement) {
        eventsCoordinator.onUpdate(hexagonBoard, scoreBeforePlacement, tilesInHand);
    }

	public JLabel getTimeLabel(){
		return this.playerRenderer.getTimeLabel();
	}

	public JLabel getMessageLabel() {
		return this.playerRenderer.getMessageLabel();
	}

	public int getSelectedIndex(){
		return this.playerRenderer.getSelectedIndex();
	}

    public List<Placement> waitAndGetUserPlacement(final HexagonBoard hexagonBoard, 
            final List<Tile> tilesInHand, final Score scoreBeforePlacement) {
        this.update(hexagonBoard,tilesInHand,scoreBeforePlacement);
        try {
            List<Placement> lp = blockingQueue.take();
            return lp;
        } catch (InterruptedException e) {
            e.printStackTrace();
            return new ArrayList<Placement>();
        }
    }

}
